package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class HbaseClientDemo {
    public static void main(String[] args) throws Exception {

        //因为hbase的连接对象需要传一个configuration，创建一个configuration
        Configuration conf = HBaseConfiguration.create();
        //设置一些参数 zk的连接地址
        conf.set("hbase.zookeeper.quorum","linux01:2181");

        //获取hbase的连接对象
        Connection conn = ConnectionFactory.createConnection(conf);

        //对于namespace进行操作 获取一个admin对象
        Admin admin = conn.getAdmin();

        TableName[] doits = admin.listTableNamesByNamespace("doit");
        for (TableName doit : doits) {
            System.out.println(doit.getNameAsString());
        }
//        admin.deleteTable(TableName.valueOf("doit:abc"));

        //创建一张表
//        TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf("doit:abc"));
        //怎么去创建这个表的描述器的构建器
/*        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("doit:abc"));

        //创建列族描述器的构建器
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder1 = ColumnFamilyDescriptorBuilder.newBuilder("f4".getBytes(StandardCharsets.UTF_8));
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder2 = ColumnFamilyDescriptorBuilder.newBuilder("f2".getBytes(StandardCharsets.UTF_8));
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder3 = ColumnFamilyDescriptorBuilder.newBuilder("f3".getBytes(StandardCharsets.UTF_8));

        //将列族描述器放在表的描述器的构建器中
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder1.build());
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder2.build());
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder3.build());


        //将表的描述器放在创建表的api中
        admin.createTable(tableDescriptorBuilder.build());*/

        //描述一下
/*        TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf("doit:abc"));
        TableName tableName = descriptor.getTableName();
        System.out.println(tableName.getNameAsString());
        ColumnFamilyDescriptor[] columnFamilies = descriptor.getColumnFamilies();
        for (ColumnFamilyDescriptor columnFamily : columnFamilies) {
            String nameAsString = columnFamily.getNameAsString();
            int timeToLive = columnFamily.getTimeToLive();
            System.out.println(nameAsString+"==>"+timeToLive);
        }*/

        //调用各种api  得到的是一个名称空间描述器
/*        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
            String name = namespaceDescriptor.getName();
            Map<String, String> config = namespaceDescriptor.getConfiguration();
            System.out.println(name);
            System.out.println(config);
        }*/

        //创建名称空间的时候，要一个名称空间描述器
//        NamespaceDescriptor java = NamespaceDescriptor.create("java").build();
/*        NamespaceDescriptor.Builder java2 = NamespaceDescriptor.create("java2");
        java2.addConfiguration("desc","this namespace from java");
        //这他妈太恶心了，谁提的需求，
        java2.addConfiguration("吐槽","api太恶心了，还不给我加工资，小涛，有点数哈");
        NamespaceDescriptor build = java2.build();
        //创建一个名称空间
        admin.createNamespace(build);*/

//        admin.deleteNamespace("java");

//        admin.modifyNamespace();

/*        TableName[] doits = admin.listTableNamesByNamespace("doit");
        for (TableName doit : doits) {
            String name = doit.getNameAsString();
            String tableName = doit.getQualifierAsString();
            System.out.println(name+","+tableName);
        }*/


    }
}
